﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CUiMFConfigurationsGridWrapper
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using EMPTYPELIBRARYLib;
using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui48;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CUiMFConfigurationsGridWrapper : TxFlexGridExtension
{
  private const int TREE_COL = 0;
  private const int X_COL = 1;
  private const int Y_COL = 2;
  private const int Z_COL = 3;
  private const int RX_COL = 4;
  private const int RY_COL = 5;
  private const int RZ_COL = 6;
  private const int VARIATION_COL = 7;
  private const int BASIC_UNIT_STRUCTURE_COL = 8;
  private const int SET_AS_VARIATION_CONTEXT_MENU_OPTION = 0;
  private const int EDIT_VARIATION_CONTEXT_MENU_OPTION = 1;
  private const int SEPARATOR = 2;
  private const int EDIT_UNIT_CONTEXT_MENU_OPTION = 3;
  private const int RELOAD_UNIT_CONTEXT_MENU_OPTION = 4;
  private const int SAVE_UNIT_CONTEXT_MENU_OPTION = 5;
  internal const string COMBO_BOX_STYLE = "ComboBox";
  internal const string READONLY_STYLE = "Readonly";
  internal const string CENTER_ALIGNED_STYLE = "CenterAligned";
  private ContextMenu m_contextMenu;
  private Point _dragAndDropStartingPoint = new Point(0, 0);
  private ArrayList m_gridRows;
  private int m_clickedRowForContextMenu;
  private ArrayList m_variationsIndexes;
  private ArrayList m_unitDescendantIndexes;
  private CApMFAdministratorManager m_adminManager;
  private int m_clickedRow;
  private int m_clickedCol;
  private Point m_clickedPoint;
  private int m_currentEditedUnit = -1;
  private bool m_configurationChanged;
  private bool m_saveUnitMode;

  internal event CUiMFConfigurationsGridWrapper.CUiMFConfigurationsGridWrapper_LoadConfigurationEventHandler EnterEditUnitMode;

  internal CUiMFConfigurationsGridWrapper()
  {
    this.InitializeComponent();
    this.m_gridRows = new ArrayList();
    this.m_variationsIndexes = new ArrayList();
    this.m_unitDescendantIndexes = new ArrayList();
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMFConfigurationsGridWrapper));
    this.m_contextMenu = new ContextMenu();
    ((ISupportInitialize) this).BeginInit();
    ((Control) this).SuspendLayout();
    ((Control) this).Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
    ((C1FlexGridBase) this).DropMode = (DropModeEnum) 1;
    ((RowColCollection) ((C1FlexGridBase) this).Rows).Count = 1;
    ((C1FlexGridBase) this).StyleInfo = componentResourceManager.GetString("$this.StyleInfo");
    ((C1FlexGridBase) this).Tree.Column = 1;
    ((Control) this).Click += new EventHandler(this.CUiMFConfigurationsGridWrapper_Click);
    ((Control) this).MouseClick += new MouseEventHandler(this.CUiMFConfigurationsGridWrapper_MouseClick);
    ((C1FlexGridBase) this).KeyPressEdit += new KeyPressEditEventHandler(this.CUiMFConfigurationsGridWrapper_KeyPressEdit);
    ((Control) this).DragEnter += new DragEventHandler(this.CUiMFConfigurationsGridWrapper_DragEnter);
    ((C1FlexGridBase) this).StartEdit += new RowColEventHandler(this.CUiMFConfigurationsGridWrapper_StartEdit);
    ((C1FlexGridBase) this).BeforeMouseDown += new BeforeMouseDownEventHandler(this.CUiMFConfigurationsGridWrapper_BeforeMouseDown);
    ((C1FlexGridBase) this).AfterEdit += new RowColEventHandler(this.CUiMFConfigurationsGridWrapper_AfterEdit);
    ((Control) this).MouseDown += new MouseEventHandler(this.CUiMFConfigurationsGridWrapper_MouseDown);
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
    ((C1FlexGridBase) this).SelChange += new EventHandler(this.CUiMFConfigurationsGridWrapper_SelChange);
    ((Control) this).MouseUp += new MouseEventHandler(this.CUiMFConfigurationsGridWrapper_MouseUp);
    ((Control) this).MouseDoubleClick += new MouseEventHandler(this.CUiMFConfigurationsGridWrapper_MouseDoubleClick);
    ((Control) this).DragOver += new DragEventHandler(this.CUiMFConfigurationsGridWrapper_DragOver);
    ((Control) this).DragDrop += new DragEventHandler(this.CUiMFConfigurationsGridWrapper_DragDrop);
    ((C1FlexGridBase) this).AfterResizeColumn += new RowColEventHandler(this.CUiMFConfigurationsGridWrapper_AfterResizeColumn);
    ((Control) this).MouseMove += new MouseEventHandler(this.CUiMFConfigurationsGridWrapper_MouseMove);
    ((C1FlexGridBase) this).BeforeEdit += new RowColEventHandler(this.CUiMFConfigurationsGridWrapper_BeforeEdit);
    ((Control) this).KeyPress += new KeyPressEventHandler(this.CUiMFConfigurationsGridWrapper_KeyPress);
    ((ISupportInitialize) this).EndInit();
    ((Control) this).ResumeLayout(false);
  }

  internal void Initialize()
  {
    ((Control) this).ForeColor = SystemColors.WindowText;
    ((C1FlexGridBase) this).DrawMode = (DrawModeEnum) 1;
    ((RowColCollection) ((C1FlexGridBase) this).Rows).Count = 1;
    ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed = 1;
    ((C1FlexGridBase) this).Tree.Style = (TreeStyleFlags) 11;
    ((C1FlexGridBase) this).Tree.LineColor = Color.Black;
    ((C1FlexGridBase) this).SelectionMode = (SelectionModeEnum) 1;
    this.InitializeGridStyles();
    this.CreateContextMenu();
  }

  internal void InitializeGridStyles()
  {
    try
    {
      CellStyle normal = ((C1FlexGridBase) this).Styles.Normal;
      CellStyle cellStyle1 = ((C1FlexGridBase) this).Styles.Add("ComboBox");
      cellStyle1.ComboList = "Variation_1";
      cellStyle1.TextAlign = (TextAlignEnum) 1;
      CellStyle cellStyle2 = ((C1FlexGridBase) this).Styles.Add("Readonly");
      cellStyle2.BackColor = Color.LightGray;
      cellStyle2.TextAlign = (TextAlignEnum) 4;
      ((C1FlexGridBase) this).Styles.Add("CenterAligned").TextAlign = (TextAlignEnum) 4;
    }
    catch (Exception ex)
    {
    }
  }

  internal void SetAdminManager(CApMFAdministratorManager adminManager)
  {
    this.m_adminManager = adminManager;
  }

  internal CApMFGridRow GetNodeFirstChild(CApMFGridRow row)
  {
    Node node = ((C1FlexGridBase) this).Rows[this.m_gridRows.IndexOf((object) row)].Node.GetNode((NodeTypeEnum) 2);
    CApMFGridRow nodeFirstChild = (CApMFGridRow) null;
    if (node != null)
      nodeFirstChild = (CApMFGridRow) this.m_gridRows[((RowCol) node.Row).Index];
    return nodeFirstChild;
  }

  internal int GetNodeFirstChildIndex(int rowIndex)
  {
    int nodeFirstChildIndex = -1;
    Node node = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 2);
    if (node != null)
      nodeFirstChildIndex = ((RowCol) node.Row).Index;
    return nodeFirstChildIndex;
  }

  internal CApMFGridRow GetNodeFirstSibling(CApMFGridRow row)
  {
    ArrayList arrayList = new ArrayList();
    return (CApMFGridRow) this.m_gridRows[((RowCol) ((C1FlexGridBase) this).Rows[this.m_gridRows.IndexOf((object) row)].Node.GetNode((NodeTypeEnum) 4).Row).Index];
  }

  internal CApMFGridRow GetNodeNextSibling(CApMFGridRow row)
  {
    ArrayList arrayList = new ArrayList();
    Node node = ((C1FlexGridBase) this).Rows[this.m_gridRows.IndexOf((object) row)].Node.GetNode((NodeTypeEnum) 6);
    CApMFGridRow nodeNextSibling = (CApMFGridRow) null;
    if (node != null)
      nodeNextSibling = (CApMFGridRow) this.m_gridRows[((RowCol) node.Row).Index];
    return nodeNextSibling;
  }

  internal int GetNodeNextSiblingIndex(int rowIndex)
  {
    int nextSiblingIndex = -1;
    Node node = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 6);
    if (node != null)
      nextSiblingIndex = ((RowCol) node.Row).Index;
    return nextSiblingIndex;
  }

  internal CApMFGridRow GetGridRow(int row) => (CApMFGridRow) this.m_gridRows[row];

  internal CApMFGridRow GetSelectedGridRow()
  {
    return (CApMFGridRow) this.m_gridRows[((C1FlexGridBase) this).RowSel];
  }

  internal bool IsPrototypeNode()
  {
    bool flag = false;
    if (((C1FlexGridBase) this).ColSel == 0 && ((C1FlexGridBase) this).RowSel >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && ((C1FlexGridBase) this).RowSel < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      flag = this.m_gridRows[((C1FlexGridBase) this).RowSel] is CApMFPrototypeGridRow;
    return flag;
  }

  internal bool IsTreeNode()
  {
    return this.IsTreeNode(((C1FlexGridBase) this).RowSel, ((C1FlexGridBase) this).ColSel);
  }

  internal bool IsTreeNode(int row, int col)
  {
    bool flag = false;
    if (col == 0 && row >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      flag = true;
    return flag;
  }

  internal void CreateRoot(string rootName, int internalId)
  {
    this.ClearGrid();
    this.m_gridRows.Add((object) "dummyRow");
    CApMFGridRow capMfGridRow = (CApMFGridRow) new CApMFCompoundResGridRow(rootName, internalId, 0, 0);
    (capMfGridRow as CApMFCompoundResGridRow).IsRoot = true;
    this.m_gridRows.Add((object) capMfGridRow);
    Row row = ((C1FlexGridBase) this).Rows.Add();
    row.IsNode = true;
    row[0] = (object) rootName;
    this.SetImage(1, capMfGridRow.Icon);
    this.SetReadOnly(1, 1);
    ((C1FlexGridBase) this).Select(1, 0);
  }

  internal int RemoveRows(int rowIndex, bool includeParent)
  {
    CellRange cellRange = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetCellRange();
    int topRow = ((CellRange) ref cellRange).TopRow;
    if (!includeParent)
      ++topRow;
    int count = ((CellRange) ref cellRange).BottomRow - topRow + 1;
    ((C1FlexGridBase) this).Rows.RemoveRange(topRow, count);
    this.m_gridRows.RemoveRange(topRow, count);
    this.UpdateVariationIndexes();
    this.UpdateUnitIndexes();
    return count;
  }

  internal void AddPrototypeNode(ITxObject newObject)
  {
    this.AddPrototypeNode(newObject, 0, ((C1FlexGridBase) this).RowSel, 0);
  }

  internal void AddPrototypeNode(ITxObject newObject, int instanceId, int rowIndex, int loadedId)
  {
    CApMFPrototypeGridRow gridRow = new CApMFPrototypeGridRow(((newObject as TxPlanningObject).PlatformServicesProvider as TxEmsServicesProvider).InternalId, instanceId, loadedId);
    string planningObjectName = TxObjectEx.GetPlanningObjectName(newObject);
    this.AddRow(rowIndex, planningObjectName, (CApMFGridRow) gridRow);
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  internal void AddVariationNode()
  {
    this.AddVariationNode("Variation", ((C1FlexGridBase) this).RowSel <= 0 || ((C1FlexGridBase) this).RowSel > ((RowColCollection) ((C1FlexGridBase) this).Rows).Count || ((C1FlexGridBase) this).ColSel != 0 ? 1 : ((C1FlexGridBase) this).RowSel, 0);
  }

  internal void AddVariationNode(string variationName, int rowIndex, int prototypeId)
  {
    this.AddVariationNode(variationName, rowIndex, prototypeId, 0);
  }

  internal void AddVariationNode(
    string variationName,
    int rowIndex,
    int prototypeId,
    int instanceId)
  {
    CApMFVariationGridRow gridRow = new CApMFVariationGridRow(variationName, prototypeId, instanceId);
    this.SetRowAsVariation(this.AddRow(rowIndex, variationName, (CApMFGridRow) gridRow));
  }

  internal void AddCompoundResourceNode(string name)
  {
    this.AddCompoundResourceNode(name, 0, ((C1FlexGridBase) this).RowSel, 0, 0);
  }

  internal void AddCompoundResourceNode(
    string name,
    int prototypeId,
    int rowIndex,
    int instanceId,
    int loadedId)
  {
    CApMFGridRow gridRow = (CApMFGridRow) new CApMFCompoundResGridRow(name, prototypeId, instanceId, loadedId);
    this.AddRow(rowIndex, name, gridRow);
  }

  internal int NodeGetNumberOfChildren(int row)
  {
    CellRange cellRange = ((C1FlexGridBase) this).Rows[row].Node.GetCellRange();
    return ((CellRange) ref cellRange).BottomRow - ((CellRange) ref cellRange).TopRow;
  }

  internal void ClearGrid()
  {
    ((C1FlexGridBase) this).Rows.RemoveRange(((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed, ((RowColCollection) ((C1FlexGridBase) this).Rows).Count - ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed);
    this.m_gridRows.Clear();
    this.m_variationsIndexes.Clear();
    this.m_unitDescendantIndexes.Clear();
    this.m_currentEditedUnit = -1;
  }

  internal void ClearSelection()
  {
    foreach (RowCol rowCol in (IEnumerable) ((C1FlexGridBase) this).Rows.Selected)
      rowCol.Selected = false;
    ((C1FlexGridBase) this).Row = -1;
  }

  internal void SetImage(int rowIndex, Image icon)
  {
    ((TxFlexGrid) this).SetObjectImage(rowIndex, 0, icon);
  }

  internal void SetRootLocation(double[] location)
  {
    ((C1FlexGridBase) this).SetData(1, 1, (object) location[0]);
    ((C1FlexGridBase) this).SetData(1, 2, (object) location[1]);
    ((C1FlexGridBase) this).SetData(1, 3, (object) location[2]);
    ((C1FlexGridBase) this).SetData(1, 4, (object) location[3]);
    ((C1FlexGridBase) this).SetData(1, 5, (object) location[4]);
    ((C1FlexGridBase) this).SetData(1, 6, (object) location[5]);
  }

  internal void SetState(int rowIndex, TxCheckInCheckOutState state)
  {
    if (!(TxApplication.ActiveDocument.PlatformGlobalServicesProvider is TxEmsGlobalServicesProvider))
      return;
    ((TxFlexGrid) this).SetCheckInCheckOutStateImage(rowIndex, 0, state);
  }

  internal bool IsCompoundResourceSelected()
  {
    return ((C1FlexGridBase) this).RowSel >= 0 && ((C1FlexGridBase) this).RowSel < this.m_gridRows.Count && this.m_gridRows[((C1FlexGridBase) this).RowSel] is CApMFCompoundResGridRow && ((C1FlexGridBase) this).ColSel == 0;
  }

  internal bool IsVariationSelected()
  {
    return ((C1FlexGridBase) this).RowSel >= 0 && ((C1FlexGridBase) this).RowSel < this.m_gridRows.Count && this.m_gridRows[((C1FlexGridBase) this).RowSel] is CApMFVariationGridRow && ((C1FlexGridBase) this).ColSel == 0;
  }

  internal bool IsRootSelected() => ((C1FlexGridBase) this).RowSel == 1;

  internal bool IsUnitDescendantSelected()
  {
    bool flag = false;
    if (((C1FlexGridBase) this).RowSel >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && ((C1FlexGridBase) this).RowSel < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      flag = this.IsDescendantOfUnit(((C1FlexGridBase) this).RowSel);
    return flag;
  }

  internal bool IsUnitSelected()
  {
    bool flag = false;
    if (((C1FlexGridBase) this).RowSel >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && ((C1FlexGridBase) this).RowSel < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count && this.m_gridRows[((C1FlexGridBase) this).RowSel] is CApMFCompoundResGridRow)
      flag = ((CApMFCompoundResGridRow) this.m_gridRows[((C1FlexGridBase) this).RowSel]).IsUnit;
    return flag;
  }

  internal void CollapseChildNode(int rowIndex)
  {
    ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 3).Collapsed = true;
  }

  internal bool CanAddChildrenToSelectedRow()
  {
    bool selectedRow = false;
    int rowSel = ((C1FlexGridBase) this).RowSel;
    if (this.EditUnitMode)
    {
      if (rowSel == this.m_currentEditedUnit || this.GetAncestorOfTypeUnitRow(rowSel) == this.m_currentEditedUnit && !this.IsUnitSelected())
        selectedRow = true;
    }
    else if (rowSel == 1 || !this.IsUnitSelected() && !this.m_unitDescendantIndexes.Contains((object) rowSel))
      selectedRow = true;
    return selectedRow;
  }

  internal bool CanDeleteSelectedRow()
  {
    if (!this.IsTreeNode() || this.IsRootSelected())
      return false;
    int ancestorOfTypeUnitRow = this.GetAncestorOfTypeUnitRow(((C1FlexGridBase) this).RowSel);
    return (this.EditUnitMode && ancestorOfTypeUnitRow == this.m_currentEditedUnit) | (!this.EditUnitMode && (!this.m_unitDescendantIndexes.Contains((object) ((C1FlexGridBase) this).RowSel) || ancestorOfTypeUnitRow == 1));
  }

  internal TxTransformation GetLocationOfSelectedRowRelativeToRoot()
  {
    return this.GetLocationOfRowRelativeToRoot(((C1FlexGridBase) this).RowSel);
  }

  internal TxTransformation GetLocationOfRowRelativeToRoot(int row)
  {
    TxTransformation rowRelativeToRoot = new TxTransformation();
    for (; row > 1; row = ((RowCol) ((C1FlexGridBase) this).Rows[row].Node.GetNode((NodeTypeEnum) 1).Row).Index)
    {
      if (this.m_gridRows[row] is CApMFLocatableGridRow)
        rowRelativeToRoot = TxTransformation.op_Multiply(((CApMFLocatableGridRow) this.m_gridRows[row]).Location, rowRelativeToRoot);
    }
    return rowRelativeToRoot;
  }

  internal void ResetVariationActiveOptions(ArrayList activeOptionsIndexes)
  {
    int index1 = 0;
    for (int index2 = 1; index2 < this.m_gridRows.Count; ++index2)
    {
      if (this.m_gridRows[index2] as CApMFGridRow is CApMFVariationGridRow)
      {
        this.SetActiveOptionsForChildVariations(index2);
        this.SaveTemporaryVariationRows(index2);
        ((CApMFVariationGridRow) this.m_gridRows[index2]).SetActiveOption((int) activeOptionsIndexes[index1]);
        string activeOption = ((CApMFVariationGridRow) this.m_gridRows[index2]).GetActiveOption();
        ((C1FlexGridBase) this).SetData(index2, 7, (object) activeOption);
        this.ReloadVariationHierarchy(index2);
        ++index1;
      }
    }
  }

  private void CUiMFConfigurationsGridWrapper_DragEnter(object sender, DragEventArgs e)
  {
  }

  private void CUiMFConfigurationsGridWrapper_DragOver(object sender, DragEventArgs e)
  {
    if (this.AllowToDrop(sender, ref e))
      e.Effect = DragDropEffects.Move;
    else
      e.Effect = DragDropEffects.None;
  }

  private void CUiMFConfigurationsGridWrapper_DragDrop(object sender, DragEventArgs e)
  {
    int mouseRow = ((C1FlexGridBase) this).MouseRow;
    if (((C1FlexGridBase) this).MouseCol != 0 || mouseRow == this.m_clickedRow || !(this.m_gridRows[mouseRow] is CApMFCompoundResGridRow) && !(this.m_gridRows[mouseRow] is CApMFVariationGridRow))
      return;
    CellRange cellRange = ((C1FlexGridBase) this).Rows[this.m_clickedRow].Node.GetCellRange();
    int count = ((CellRange) ref cellRange).BottomRow - ((CellRange) ref cellRange).TopRow + 1;
    ArrayList c = new ArrayList((ICollection) this.m_gridRows.GetRange(((CellRange) ref cellRange).TopRow, count).ToArray());
    this.m_gridRows.RemoveRange(((CellRange) ref cellRange).TopRow, count);
    this.m_gridRows.InsertRange(this.m_clickedRow <= mouseRow ? mouseRow - count + 1 : mouseRow + 1, (ICollection) c);
    ((C1FlexGridBase) this).Rows[this.m_clickedRow].Node.Move((NodeMoveEnum) 6, ((C1FlexGridBase) this).Rows[mouseRow].Node);
    this.UpdateVariationIndexes();
    this.UpdateUnitIndexes();
    e.Effect = DragDropEffects.Move;
    this.m_configurationChanged = true;
  }

  private void CUiMFConfigurationsGridWrapper_SelChange(object sender, EventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_StartEdit(object sender, RowColEventArgs e)
  {
    if (e.Col != 7 || !this.m_variationsIndexes.Contains((object) e.Row))
      return;
    this.SetVariationOptions(e.Row);
  }

  private void CUiMFConfigurationsGridWrapper_MouseClick(object sender, MouseEventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_MouseDoubleClick(object sender, MouseEventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_BeforeMouseDown(
    object sender,
    BeforeMouseDownEventArgs e)
  {
  }

  private void CUiMFConfigurationsGridWrapper_Click(object sender, EventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_OwnerDrawCell(object sender, OwnerDrawCellEventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell -= new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
    ((C1FlexGridBase) this).SelChange -= new EventHandler(this.CUiMFConfigurationsGridWrapper_SelChange);
    for (int index = 0; index < this.m_variationsIndexes.Count; ++index)
      this.SetRowAsVariation((int) this.m_variationsIndexes[index]);
    if (((RowColCollection) ((C1FlexGridBase) this).Rows).Count > ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed)
      this.SetReadOnly(1, 1);
    if (this.EditUnitMode)
    {
      for (int index = 2; index < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++index)
      {
        if (this.GetAncestorOfTypeUnitRow(index) != this.m_currentEditedUnit)
          this.SetReadOnly(index, index);
        else if (!this.m_variationsIndexes.Contains((object) index))
          this.SetCoordinatesInCenter(index, 1, index, 6);
      }
    }
    else
    {
      for (int index = 2; index < ((RowColCollection) ((C1FlexGridBase) this).Rows).Count; ++index)
      {
        if (this.m_unitDescendantIndexes.Contains((object) index))
          this.SetReadOnly(index, index);
        else if (!this.m_variationsIndexes.Contains((object) index))
          this.SetCoordinatesInCenter(index, 1, index, 6);
      }
    }
    CellStyle style = ((C1FlexGridBase) this).Styles["CenterAligned"];
    CellRange cellRange = ((C1FlexGridBase) this).GetCellRange(0, 1, 0, 8);
    ((CellRange) ref cellRange).Style = style;
    ((C1FlexGridBase) this).SelChange += new EventHandler(this.CUiMFConfigurationsGridWrapper_SelChange);
  }

  private void CUiMFConfigurationsGridWrapper_MouseUp(object sender, MouseEventArgs e)
  {
    if (e.Button == MouseButtons.Right)
    {
      ((Control) this).PointToClient(((Control) sender).PointToScreen(new Point(e.X, e.Y)));
      HitTestInfo hitTestInfo = ((C1FlexGridBase) this).HitTest(e.X, e.Y);
      if (!this.IsTreeNode(((HitTestInfo) ref hitTestInfo).Row, ((HitTestInfo) ref hitTestInfo).Column))
        return;
      this.m_clickedRowForContextMenu = ((HitTestInfo) ref hitTestInfo).Row;
      if (!this.m_variationsIndexes.Contains((object) ((HitTestInfo) ref hitTestInfo).Row) || !this.EditUnitMode && this.m_unitDescendantIndexes.Contains((object) ((HitTestInfo) ref hitTestInfo).Row) || this.EditUnitMode && this.GetAncestorOfTypeUnitRow(((HitTestInfo) ref hitTestInfo).Row) != this.m_currentEditedUnit)
        this.m_contextMenu.MenuItems[1].Enabled = false;
      else
        this.m_contextMenu.MenuItems[1].Enabled = true;
      if (!this.EditUnitMode && this.m_unitDescendantIndexes.Contains((object) ((HitTestInfo) ref hitTestInfo).Row) || this.m_adminManager.ConfigurationData is CApMFFixtureBaseTypeData || ((HitTestInfo) ref hitTestInfo).Row == 1 || this.EditUnitMode && this.GetAncestorOfTypeUnitRow(((HitTestInfo) ref hitTestInfo).Row) != this.m_currentEditedUnit)
        this.m_contextMenu.MenuItems[0].Enabled = false;
      else
        this.m_contextMenu.MenuItems[0].Enabled = true;
      if (this.IsUnitSelected() && !this.EditUnitMode && ((HitTestInfo) ref hitTestInfo).Row > 1)
        this.m_contextMenu.MenuItems[3].Enabled = true;
      else
        this.m_contextMenu.MenuItems[3].Enabled = false;
      if (this.EditUnitMode && this.m_clickedRowForContextMenu == this.m_currentEditedUnit)
        this.m_contextMenu.MenuItems[5].Enabled = true;
      else
        this.m_contextMenu.MenuItems[5].Enabled = false;
      if (this.EditUnitMode && this.m_clickedRowForContextMenu == this.m_currentEditedUnit)
        this.m_contextMenu.MenuItems[4].Enabled = true;
      else
        this.m_contextMenu.MenuItems[4].Enabled = false;
      this.m_contextMenu.Show((Control) this, new Point(e.X, e.Y));
    }
    else
      ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_MouseMove(object sender, MouseEventArgs e)
  {
    if (e.Button != MouseButtons.Left || this.m_clickedRow != ((C1FlexGridBase) this).MouseRow || this.m_clickedCol != ((C1FlexGridBase) this).MouseCol || this.m_clickedRow <= 1 || this.m_clickedCol != 0 || Math.Abs(this.m_clickedPoint.X - e.X) + Math.Abs(this.m_clickedPoint.Y - e.Y) <= 5)
      return;
    int num = (int) ((Control) this).DoDragDrop((object) ((C1FlexGridBase) this).Rows[this.m_clickedRow].Node, DragDropEffects.Move);
  }

  private void CUiMFConfigurationsGridWrapper_MouseDown(object sender, MouseEventArgs e)
  {
    this.m_clickedRow = ((C1FlexGridBase) this).MouseRow;
    this.m_clickedCol = ((C1FlexGridBase) this).MouseCol;
    this.m_clickedPoint = new Point(e.X, e.Y);
    if (e.Button != MouseButtons.Right)
      return;
    this.RClickSelectRow(e.X, e.Y);
  }

  private void CUiMFConfigurationsGridWrapper_AfterEdit(object sender, RowColEventArgs e)
  {
    if (e.Row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed || e.Row >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return;
    object data = ((C1FlexGridBase) this).GetData(e.Row, e.Col);
    if (e.Col == 7 && !data.ToString().Equals("") && !((CApMFVariationGridRow) this.m_gridRows[e.Row]).GetActiveOption().Equals(data.ToString()))
    {
      Cursor current = Cursor.Current;
      Cursor.Current = Cursors.WaitCursor;
      ((ScrollableControl) this).BeginUpdate();
      this.SetActiveOptionsForChildVariations(e.Row);
      this.SaveTemporaryVariationRows(e.Row);
      ((CApMFVariationGridRow) this.m_gridRows[e.Row]).SetActiveOption(data.ToString());
      ((C1FlexGridBase) this).SetData(e.Row, e.Col, data);
      ((ScrollableControl) this).EndUpdate();
      this.ReloadVariationHierarchy(e.Row);
      Cursor.Current = current;
    }
    else if (e.Col >= 1 && e.Col <= 3)
    {
      if (((C1FlexGridBase) this).Rows[e.Row][e.Col] == null)
        ((C1FlexGridBase) this).Rows[e.Row][e.Col] = (object) "0";
      double linearMultiplier = TxApplication.Options.Units.LinearMultiplier;
      TxVector txVector = new TxVector((double) ((C1FlexGridBase) this).Rows[e.Row][1] * linearMultiplier, (double) ((C1FlexGridBase) this).Rows[e.Row][2] * linearMultiplier, (double) ((C1FlexGridBase) this).Rows[e.Row][3] * linearMultiplier);
      ((CApMFLocatableGridRow) this.m_gridRows[e.Row]).Location.Translation = txVector;
    }
    else if (e.Col >= 4 && e.Col <= 6)
    {
      if (((C1FlexGridBase) this).Rows[e.Row][e.Col] == null)
        ((C1FlexGridBase) this).Rows[e.Row][e.Col] = (object) "0";
      double angularMultiplier = TxApplication.Options.Units.AngularMultiplier;
      TxVector txVector = new TxVector((double) ((C1FlexGridBase) this).Rows[e.Row][4] * angularMultiplier, (double) ((C1FlexGridBase) this).Rows[e.Row][5] * angularMultiplier, (double) ((C1FlexGridBase) this).Rows[e.Row][6] * angularMultiplier);
      ((CApMFLocatableGridRow) this.m_gridRows[e.Row]).Location.RotationRPY_XYZ = txVector;
    }
    else if (e.Col == 0)
    {
      if (((C1FlexGridBase) this).Rows[e.Row][e.Col] != null && !((C1FlexGridBase) this).Rows[e.Row][e.Col].ToString().Equals(""))
        ((CApMFGridRow) this.m_gridRows[e.Row]).Name = ((C1FlexGridBase) this).Rows[e.Row][e.Col].ToString();
      else
        ((C1FlexGridBase) this).Rows[e.Row][e.Col] = (object) ((CApMFGridRow) this.m_gridRows[e.Row]).Name;
    }
    else if (e.Col == 8 && this.m_gridRows[e.Row] is CApMFLocatableGridRow)
    {
      bool flag = ((C1FlexGridBase) this).GetCellCheck(e.Row, e.Col) == 1;
      ((CApMFLocatableGridRow) this.m_gridRows[e.Row]).BasicUnitStructure = flag;
      if (flag)
      {
        this.SetParentsBasicUnitStructure(e.Row, true);
        this.SetDescendantsBasicUnitStructure(e.Row, true);
      }
      else
      {
        int index = ((RowCol) ((C1FlexGridBase) this).Rows[e.Row].Node.GetNode((NodeTypeEnum) 1).Row).Index;
        if (index > ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && !this.descendantsHaveBasicUnitStrcture(index))
          this.SetParentsBasicUnitStructure(e.Row, false);
        this.SetDescendantsBasicUnitStructure(e.Row, false);
      }
    }
    this.m_configurationChanged = true;
  }

  private void SetDescendantsBasicUnitStructure(int row, bool basicUnitStructure)
  {
    CellRange cellRange = ((C1FlexGridBase) this).Rows[row].Node.GetCellRange();
    for (int index = ((CellRange) ref cellRange).TopRow + 1; index <= ((CellRange) ref cellRange).BottomRow; ++index)
    {
      if (this.m_gridRows[index] is CApMFLocatableGridRow)
      {
        if (basicUnitStructure)
          ((C1FlexGridBase) this).SetCellCheck(index, 8, (CheckEnum) 1);
        else
          ((C1FlexGridBase) this).SetCellCheck(index, 8, (CheckEnum) 2);
        ((CApMFLocatableGridRow) this.m_gridRows[index]).BasicUnitStructure = basicUnitStructure;
      }
    }
  }

  private void SetParentsBasicUnitStructure(int row, bool basicUnitStructure)
  {
    for (int index = ((RowCol) ((C1FlexGridBase) this).Rows[row].Node.GetNode((NodeTypeEnum) 1).Row).Index; index > ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed; index = ((RowCol) ((C1FlexGridBase) this).Rows[index].Node.GetNode((NodeTypeEnum) 1).Row).Index)
    {
      if (this.m_gridRows[index] is CApMFLocatableGridRow)
      {
        if (basicUnitStructure)
          ((C1FlexGridBase) this).SetCellCheck(index, 8, (CheckEnum) 1);
        else
          ((C1FlexGridBase) this).SetCellCheck(index, 8, (CheckEnum) 2);
        ((CApMFLocatableGridRow) this.m_gridRows[index]).BasicUnitStructure = basicUnitStructure;
      }
    }
  }

  private bool descendantsHaveBasicUnitStrcture(int parentRow)
  {
    bool flag = false;
    for (Node node = ((C1FlexGridBase) this).Rows[parentRow].Node.GetNode((NodeTypeEnum) 2); node != null && !flag; node = ((C1FlexGridBase) this).Rows[((RowCol) node.Row).Index].Node.GetNode((NodeTypeEnum) 6))
    {
      if (this.m_gridRows[((RowCol) node.Row).Index] is CApMFLocatableGridRow gridRow)
        flag = gridRow.BasicUnitStructure;
    }
    return flag;
  }

  private void CUiMFConfigurationsGridWrapper_AfterResizeColumn(object sender, RowColEventArgs e)
  {
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
  }

  private void CUiMFConfigurationsGridWrapper_BeforeEdit(object sender, RowColEventArgs e)
  {
    if (e.Row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed)
      return;
    switch (e.Col)
    {
      case 0:
        int ancestorOfTypeUnitRow = this.GetAncestorOfTypeUnitRow(e.Row);
        if (this.m_gridRows[e.Row] is CApMFPrototypeGridRow || e.Row == 1)
        {
          e.Cancel = true;
          break;
        }
        if (this.EditUnitMode && ancestorOfTypeUnitRow != this.m_currentEditedUnit)
        {
          e.Cancel = true;
          break;
        }
        if ((!this.m_unitDescendantIndexes.Contains((object) e.Row) || this.EditUnitMode) && (!this.m_unitDescendantIndexes.Contains((object) e.Row) || !this.EditUnitMode || ancestorOfTypeUnitRow == this.m_currentEditedUnit))
          break;
        e.Cancel = true;
        break;
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        if (this.EditUnitMode && this.GetAncestorOfTypeUnitRow(e.Row) != this.m_currentEditedUnit)
        {
          e.Cancel = true;
          break;
        }
        if (!this.EditUnitMode && (this.m_unitDescendantIndexes.Contains((object) e.Row) || this.m_variationsIndexes.Contains((object) e.Row) || e.Row == 1))
        {
          e.Cancel = true;
          break;
        }
        ((RowCol) ((C1FlexGridBase) this).Cols[e.Col]).DataType = typeof (double);
        break;
      case 7:
        if (this.m_variationsIndexes.Contains((object) e.Row) && (!this.EditUnitMode || this.GetAncestorOfTypeUnitRow(e.Row) == this.m_currentEditedUnit))
          break;
        e.Cancel = true;
        break;
      case 8:
        if (e.Row != ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed)
          break;
        e.Cancel = true;
        break;
    }
  }

  private void CUiMFConfigurationsGridWrapper_KeyPressEdit(object sender, KeyPressEditEventArgs e)
  {
    if (e.KeyChar != '\r')
      return;
    ((C1FlexGridBase) this).MoveSelection((MoveCursorEnum) 4, false);
  }

  private void CUiMFConfigurationsGridWrapper_KeyPress(object sender, KeyPressEventArgs e)
  {
    if (e.KeyChar != '\r')
      return;
    ((C1FlexGridBase) this).MoveSelection((MoveCursorEnum) 4, false);
    ((C1FlexGridBase) this).FinishEditing();
  }

  private void LoadAttributesToGrid(int rowIndex)
  {
    if (!(this.m_gridRows[rowIndex] is CApMFLocatableGridRow))
      return;
    CApMFLocatableGridRow gridRow = this.m_gridRows[rowIndex] as CApMFLocatableGridRow;
    if (!(this.m_adminManager.ConfigurationData is CApMFFixtureBaseTypeData))
    {
      if (gridRow.BasicUnitStructure)
        ((C1FlexGridBase) this).SetCellCheck(rowIndex, 8, (CheckEnum) 1);
      else
        ((C1FlexGridBase) this).SetCellCheck(rowIndex, 8, (CheckEnum) 2);
    }
    TxVector translation = gridRow.Location.Translation;
    TxVector rotationRpyXyz = gridRow.Location.RotationRPY_XYZ;
    double angularMultiplier = TxApplication.Options.Units.AngularMultiplier;
    int afterDecimalPoint1 = TxApplication.Options.Units.AngularDigitsAfterDecimalPoint;
    double linearMultiplier = TxApplication.Options.Units.LinearMultiplier;
    int afterDecimalPoint2 = TxApplication.Options.Units.LinearDigitsAfterDecimalPoint;
    TxUnitsOptions units = TxApplication.Options.Units;
    string str = "F" + afterDecimalPoint1.ToString();
    ((C1FlexGridBase) this).Rows[rowIndex][1] = (object) (Math.Round(translation.X, afterDecimalPoint2) / linearMultiplier);
    ((C1FlexGridBase) this).Rows[rowIndex][2] = (object) (Math.Round(translation.Y, afterDecimalPoint2) / linearMultiplier);
    ((C1FlexGridBase) this).Rows[rowIndex][3] = (object) (Math.Round(translation.Z, afterDecimalPoint2) / linearMultiplier);
    ((C1FlexGridBase) this).Rows[rowIndex][4] = (object) Math.Round(rotationRpyXyz.X / angularMultiplier, afterDecimalPoint1);
    ((C1FlexGridBase) this).Rows[rowIndex][5] = (object) Math.Round(rotationRpyXyz.Y / angularMultiplier, afterDecimalPoint1);
    ((C1FlexGridBase) this).Rows[rowIndex][6] = (object) Math.Round(rotationRpyXyz.Z / angularMultiplier, afterDecimalPoint1);
  }

  private bool AllowToDrop(object sender, ref DragEventArgs e)
  {
    bool drop = false;
    Point client = ((Control) sender).PointToClient(new Point(e.X, e.Y));
    HitTestInfo hitTestInfo = ((C1FlexGridBase) this).HitTest(client.X, client.Y);
    bool flag1 = ((HitTestInfo) ref hitTestInfo).Column == 0;
    bool flag2 = this.m_gridRows[((HitTestInfo) ref hitTestInfo).Row] is CApMFCompoundResGridRow || this.m_gridRows[((HitTestInfo) ref hitTestInfo).Row] is CApMFVariationGridRow;
    bool flag3 = this.IsNodeDescendantOf(this.m_clickedRow, ((HitTestInfo) ref hitTestInfo).Row);
    bool flag4 = this.m_unitDescendantIndexes.Contains((object) this.m_clickedRow);
    bool flag5 = this.m_unitDescendantIndexes.Contains((object) ((HitTestInfo) ref hitTestInfo).Row);
    bool flag6 = this.m_gridRows[((HitTestInfo) ref hitTestInfo).Row] is CApMFCompoundResGridRow && ((CApMFCompoundResGridRow) this.m_gridRows[((HitTestInfo) ref hitTestInfo).Row]).IsUnit;
    if (flag1 & flag2 && !flag3 && !flag4 && !flag5 && !flag6)
      drop = true;
    return drop;
  }

  private void CreateContextMenu()
  {
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    MenuItem menuItem3 = new MenuItem();
    MenuItem menuItem4 = new MenuItem();
    MenuItem menuItem5 = new MenuItem();
    MenuItem menuItem6 = new MenuItem();
    this.m_contextMenu.MenuItems.AddRange(new MenuItem[6]
    {
      menuItem1,
      menuItem2,
      menuItem6,
      menuItem3,
      menuItem5,
      menuItem4
    });
    menuItem1.Index = 0;
    menuItem1.Text = this.GetResourceManager().GetString("SET_AS_VARIATION_CONTEXT_MENU");
    menuItem2.Index = 1;
    menuItem2.Text = this.GetResourceManager().GetString("EDIT_VARIATION_CONTEXT_MENU");
    menuItem3.Index = 3;
    menuItem3.Text = this.GetResourceManager().GetString("EDIT_UNIT_CONTEXT_MENU");
    menuItem4.Index = 5;
    menuItem4.Text = this.GetResourceManager().GetString("SAVE_UNIT_CONTEXT_MENU");
    menuItem5.Index = 4;
    menuItem5.Text = this.GetResourceManager().GetString("RELOAD_UNIT_CONTEXT_MENU");
    menuItem6.Index = 2;
    menuItem6.Text = "-";
    menuItem1.Click += new EventHandler(this.menuItemSetAsVariation_Click);
    menuItem2.Click += new EventHandler(this.menuItemEditVariation_Click);
    menuItem3.Click += new EventHandler(this.menuItemEditUnit_Click);
    menuItem5.Click += new EventHandler(this.menuItemReloadUnit_Click);
    menuItem4.Click += new EventHandler(this.menuItemSaveUnit_Click);
  }

  private void menuItemReloadUnit_Click(object sender, EventArgs e)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    ((ScrollableControl) this).BeginUpdate();
    this.m_saveUnitMode = true;
    this.m_adminManager.LoadConfiguration((TxPlanningObject) new TxEmsGlobalServicesProvider().GetObjectByInternalId(((CApMFGridRow) this.m_gridRows[1]).PrototypeId), 1);
    this.m_saveUnitMode = false;
    ((ScrollableControl) this).EndUpdate();
    Cursor.Current = current;
  }

  private void menuItemSaveUnit_Click(object sender, EventArgs e)
  {
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    ((ScrollableControl) this).BeginUpdate();
    this.m_saveUnitMode = true;
    ArrayList arrayList1 = new ArrayList();
    ArrayList arrayList2 = new ArrayList();
    ArrayList arrayList3 = new ArrayList();
    CApMFCompoundResGridRow gridRow = this.m_gridRows[this.m_currentEditedUnit] as CApMFCompoundResGridRow;
    int ancestorId = this.GetAncestorId(this.m_currentEditedUnit);
    EmpObjectKey parent;
    parent.objectId = ancestorId;
    this.m_adminManager.saveToEMS((CApMFGridRow) gridRow, parent);
    this.m_adminManager.LoadConfiguration((TxPlanningObject) new TxEmsGlobalServicesProvider().GetObjectByInternalId(((CApMFGridRow) this.m_gridRows[1]).PrototypeId), 1);
    this.m_saveUnitMode = false;
    ((ScrollableControl) this).EndUpdate();
    Cursor.Current = current;
  }

  private void menuItemEditUnit_Click(object sender, EventArgs e)
  {
    ((ScrollableControl) this).BeginUpdate();
    this.m_currentEditedUnit = ((C1FlexGridBase) this).RowSel;
    ((C1FlexGridBase) this).OwnerDrawCell += new OwnerDrawCellEventHandler(this.CUiMFConfigurationsGridWrapper_OwnerDrawCell);
    ((C1FlexGridBase) this).Select(((C1FlexGridBase) this).RowSel, ((C1FlexGridBase) this).ColSel);
    this.EnterEditUnitMode();
    ((ScrollableControl) this).EndUpdate();
  }

  private void menuItemSetAsVariation_Click(object sender, EventArgs e)
  {
    CApMFVariationGridRow variationGridRow = new CApMFVariationGridRow("Variation", 0, 0);
    this.m_gridRows.Insert(this.m_clickedRowForContextMenu, (object) variationGridRow);
    ((C1FlexGridBase) this).Rows[this.m_clickedRowForContextMenu + 1].Node.Move((NodeMoveEnum) 6, ((C1FlexGridBase) this).Rows[this.m_clickedRowForContextMenu].Node.AddNode((NodeTypeEnum) 7, (object) "Variation"));
    if (this.m_variationsIndexes.Contains((object) this.m_clickedRowForContextMenu))
      this.m_variationsIndexes[this.m_variationsIndexes.IndexOf((object) this.m_clickedRowForContextMenu)] = (object) (this.m_clickedRowForContextMenu + 1);
    this.SetRowAsVariation(this.m_clickedRowForContextMenu);
    this.SetImage(this.m_clickedRowForContextMenu, variationGridRow.Icon);
    this.UpdateVariationIndexes();
    this.UpdateUnitIndexes();
    this.ConfigurationChanged = true;
  }

  private int GetAncestorId(int rowIndex)
  {
    int ancestorId = 0;
    int index = ((RowCol) ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 1).Row).Index;
    if (this.m_gridRows[index] is CApMFVariationGridRow)
    {
      string optionName = ((C1FlexGridBase) this).GetData(index, 7).ToString();
      ancestorId = ((CApMFVariationGridRow) this.m_gridRows[index]).GetInternalIdForOptionName(optionName);
    }
    else if (this.m_gridRows[index] is CApMFLocatableGridRow)
      ancestorId = ((CApMFGridRow) this.m_gridRows[index]).PrototypeId;
    return ancestorId;
  }

  private int GetAncestorOfTypeUnitRow(int rowIndex)
  {
    int ancestorOfTypeUnitRow = -1;
    bool flag = false;
    if (rowIndex > 1)
    {
      for (int index = ((RowCol) ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 1).Row).Index; index > 1 && !flag; index = ((RowCol) ((C1FlexGridBase) this).Rows[index].Node.GetNode((NodeTypeEnum) 1).Row).Index)
      {
        if (this.m_gridRows[index] is CApMFCompoundResGridRow && (this.m_gridRows[index] as CApMFCompoundResGridRow).IsUnit)
        {
          flag = true;
          ancestorOfTypeUnitRow = index;
        }
      }
    }
    return ancestorOfTypeUnitRow;
  }

  private void SetRowAsVariation(int rowIndex)
  {
    this.SetVariationOptions(rowIndex);
    if (!this.m_variationsIndexes.Contains((object) rowIndex))
      this.m_variationsIndexes.Add((object) rowIndex);
    this.SetReadOnly(rowIndex, rowIndex);
  }

  private void menuItemEditVariation_Click(object sender, EventArgs e)
  {
    CApMFVariationGridRow gridRow = (CApMFVariationGridRow) this.m_gridRows[this.m_clickedRowForContextMenu];
    CUiMFVariationsOptionsDlg variationsOptionsDlg = new CUiMFVariationsOptionsDlg(gridRow.Name, gridRow.VariationOptions);
    if (((Form) variationsOptionsDlg).ShowDialog() != DialogResult.OK)
      return;
    this.SetActiveOptionsForChildVariations(this.m_clickedRowForContextMenu);
    this.SaveTemporaryVariationRows(this.m_clickedRowForContextMenu);
    gridRow.VariationOptions = variationsOptionsDlg.Options;
    gridRow.ValidateActiveOption();
    this.SetVariationOptions(this.m_clickedRowForContextMenu);
    this.ReloadVariationHierarchy(this.m_clickedRowForContextMenu);
    this.ConfigurationChanged = true;
  }

  private void SetVariationOptions(int row)
  {
    CApMFVariationGridRow gridRow = (CApMFVariationGridRow) this.m_gridRows[row];
    CellStyle style = ((C1FlexGridBase) this).Styles["ComboBox"];
    style.ComboList = "";
    for (int index = 0; index < gridRow.VariationOptions.Count; ++index)
    {
      style.ComboList += ((CApMFVariationOption) gridRow.VariationOptions[index]).Name;
      style.ComboList += "|";
    }
    ((C1FlexGridBase) this).SetCellStyle(row, 7, style);
    ((C1FlexGridBase) this).SetData(row, 7, (object) gridRow.GetActiveOption());
  }

  private void SetActiveOptionsForChildVariations(int rowIndex)
  {
    CellRange cellRange = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetCellRange();
    for (int index = ((CellRange) ref cellRange).TopRow + 1; index <= ((CellRange) ref cellRange).BottomRow; ++index)
    {
      if (this.m_gridRows[index] is CApMFVariationGridRow)
      {
        CApMFVariationGridRow gridRow = (CApMFVariationGridRow) this.m_gridRows[index];
        if (gridRow.GetActiveOptionIndex() > 0)
        {
          this.SaveTemporaryVariationRows(index);
          gridRow.SetActiveOption(0);
          ((C1FlexGridBase) this).SetData(index, 7, (object) gridRow.GetActiveOption());
          this.ReloadVariationHierarchy(index);
          break;
        }
      }
    }
  }

  private void SaveTemporaryVariationRows(int rowIndex)
  {
    CellRange cellRange = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetCellRange();
    ArrayList parentRows = new ArrayList();
    ArrayList gridRows = new ArrayList();
    ArrayList parentGridRows = new ArrayList();
    for (int index1 = ((CellRange) ref cellRange).TopRow + 1; index1 <= ((CellRange) ref cellRange).BottomRow; ++index1)
    {
      int index2 = ((RowCol) ((C1FlexGridBase) this).Rows[index1].Node.GetNode((NodeTypeEnum) 1).Row).Index;
      parentRows.Add((object) (index2 - rowIndex));
      gridRows.Add(this.m_gridRows[index1]);
      if (this.m_gridRows[index2] is CApMFVariationGridRow)
        parentGridRows.Add((object) new ArrayList(2)
        {
          this.m_gridRows[index2],
          (object) ((CApMFVariationGridRow) this.m_gridRows[index2]).GetActiveOption()
        });
      else
        parentGridRows.Add(this.m_gridRows[index2]);
    }
    if (parentRows.Count <= 0)
      return;
    ((CApMFVariationGridRow) this.m_gridRows[rowIndex]).SetOptionRows(parentRows, gridRows, parentGridRows);
  }

  private void ReloadVariationHierarchy(int rowIndex)
  {
    this.RemoveRows(rowIndex, false);
    string str = ((C1FlexGridBase) this).GetData(rowIndex, 7).ToString();
    CApMFVariationGridRow gridRow = this.m_gridRows[rowIndex] as CApMFVariationGridRow;
    int internalIdForOptionName = gridRow.GetInternalIdForOptionName(str);
    if (internalIdForOptionName > 0)
    {
      foreach (ITxPlanningObject objectDescendant in (Collection<ITxObject>) CApMFAdminUtils.GetPlanningObjectDescendants((TxPlanningObject) (TxApplication.ActiveDocument.PlatformGlobalServicesProvider as TxEmsGlobalServicesProvider).GetObjectByInternalId(internalIdForOptionName)))
      {
        TxEmsServicesProvider servicesProvider = objectDescendant.PlatformServicesProvider as TxEmsServicesProvider;
        this.m_adminManager.LoadConfigurationChildren((TxPlanningObject) CApMFAdminUtils.GetPrototype(objectDescendant), servicesProvider.InternalId, rowIndex);
      }
    }
    ArrayList rows = new ArrayList();
    ArrayList gridRows = new ArrayList();
    gridRow.GetGridRows(str, ref rows, ref gridRows);
    for (int index = 0; index < rows.Count; ++index)
    {
      bool flag = true;
      if (((CApMFGridRow) gridRows[index]).InstanceId > 0)
      {
        int childWithInternalId = this.GetIndexOfChildWithInternalId(rowIndex + (int) rows[index], ((CApMFGridRow) gridRows[index]).InstanceId);
        if (childWithInternalId > -1)
        {
          this.m_gridRows[childWithInternalId] = gridRows[index];
          this.LoadAttributesToGrid(childWithInternalId);
          flag = false;
        }
      }
      if (flag)
      {
        int rowIndex1 = this.AddRow(rowIndex + (int) rows[index], ((CApMFGridRow) gridRows[index]).Name, (CApMFGridRow) gridRows[index]);
        if (gridRows[index] is CApMFVariationGridRow)
          this.SetRowAsVariation(rowIndex1);
      }
    }
    this.UpdateVariationIndexes();
    this.UpdateUnitIndexes();
  }

  private int GetIndexOfChildWithInternalId(int rowIndex, int internalId)
  {
    int childWithInternalId = -1;
    if (rowIndex >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return childWithInternalId;
    CellRange cellRange = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetCellRange();
    for (int index = ((CellRange) ref cellRange).TopRow + 1; index <= ((CellRange) ref cellRange).BottomRow; ++index)
    {
      if (((CApMFGridRow) this.m_gridRows[index]).InstanceId == internalId)
      {
        childWithInternalId = index;
        break;
      }
    }
    return childWithInternalId;
  }

  private void AddComboBoxForVariation(int row)
  {
    CellStyle style = ((C1FlexGridBase) this).Styles["ComboBox"];
    style.ComboList = "Variation_1";
    ((C1FlexGridBase) this).SetCellStyle(row, 7, style);
    ((C1FlexGridBase) this).SetData(row, 7, (object) "Variation_1");
  }

  private void RClickSelectRow(int x, int y)
  {
    HitTestInfo hitTestInfo = ((C1FlexGridBase) this).HitTest(x, y);
    if (((HitTestInfo) ref hitTestInfo).Row < ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed || ((HitTestInfo) ref hitTestInfo).Row >= ((RowColCollection) ((C1FlexGridBase) this).Rows).Count)
      return;
    ((C1FlexGridBase) this).Select(((HitTestInfo) ref hitTestInfo).Row, ((HitTestInfo) ref hitTestInfo).Column);
  }

  private bool IsDescendantOfUnit(int row)
  {
    if (row == ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed)
      return false;
    int index = ((RowCol) ((C1FlexGridBase) this).Rows[row].Node.GetNode((NodeTypeEnum) 1).Row).Index;
    return this.m_gridRows[index] is CApMFCompoundResGridRow && index != ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed && ((CApMFCompoundResGridRow) this.m_gridRows[index]).IsUnit || this.IsDescendantOfUnit(index);
  }

  private bool IsNodeDescendantOf(int parentRow, int childRow)
  {
    if (childRow == ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed)
      return false;
    if (parentRow == childRow)
      return true;
    int index = ((RowCol) ((C1FlexGridBase) this).Rows[childRow].Node.GetNode((NodeTypeEnum) 1).Row).Index;
    return this.IsNodeDescendantOf(parentRow, index);
  }

  private void UpdateVariationIndexes()
  {
    this.m_variationsIndexes.Clear();
    for (int index = ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed; index < this.m_gridRows.Count; ++index)
    {
      if (this.m_gridRows[index] is CApMFVariationGridRow)
        this.m_variationsIndexes.Add((object) index);
    }
  }

  private void UpdateUnitIndexes()
  {
    this.m_unitDescendantIndexes.Clear();
    for (int row = ((RowColCollection) ((C1FlexGridBase) this).Rows).Fixed; row < this.m_gridRows.Count; ++row)
    {
      if (this.IsDescendantOfUnit(row))
        this.m_unitDescendantIndexes.Add((object) row);
    }
  }

  private int AddRow(int rowIndex, string name, CApMFGridRow gridRow)
  {
    Node node = ((C1FlexGridBase) this).Rows[rowIndex].Node.AddNode((NodeTypeEnum) 3, (object) name);
    this.m_gridRows.Insert(((RowCol) node.Row).Index, (object) gridRow);
    bool flag = this.IsDescendantOfUnit(((RowCol) node.Row).Index);
    this.LoadAttributesToGrid(((RowCol) node.Row).Index);
    if (flag)
    {
      this.m_unitDescendantIndexes.Add((object) ((RowCol) node.Row).Index);
      this.SetReadOnly(((RowCol) node.Row).Index, ((RowCol) node.Row).Index);
    }
    else
      this.SetCoordinatesInCenter(((RowCol) node.Row).Index, 1, ((RowCol) node.Row).Index, 6);
    this.UpdateVariationIndexes();
    this.UpdateUnitIndexes();
    this.SetImage(((RowCol) node.Row).Index, gridRow.Icon);
    return ((RowCol) node.Row).Index;
  }

  private void SetReadOnly(int topRow, int bottomRow)
  {
    CellStyle style = ((C1FlexGridBase) this).Styles["Readonly"];
    CellRange cellRange = ((C1FlexGridBase) this).GetCellRange(topRow, 1, bottomRow, 6);
    ((CellRange) ref cellRange).Style = style;
  }

  private ArrayList GetActiveOptions()
  {
    ArrayList activeOptions = new ArrayList();
    for (int index = 1; index < this.m_gridRows.Count; ++index)
    {
      if (this.m_gridRows[index] is CApMFVariationGridRow)
        activeOptions.Add((object) ((CApMFVariationGridRow) this.m_gridRows[index]).GetActiveOptionIndex());
    }
    return activeOptions;
  }

  private void SetCoordinatesInCenter(int topRow, int leftCol, int bottomRow, int rightCol)
  {
    CellStyle style = ((C1FlexGridBase) this).Styles["CenterAligned"];
    CellRange cellRange = ((C1FlexGridBase) this).GetCellRange(topRow, leftCol, bottomRow, rightCol);
    ((CellRange) ref cellRange).Style = style;
  }

  private ResourceManager GetResourceManager()
  {
    if (CApMFAdminUtils.m_resourceManager == null)
      CApMFAdminUtils.m_resourceManager = new ResourceManager("ModularFixture.ConfigurationAdministrator.Res.StringTable", ((object) this).GetType().Assembly);
    return CApMFAdminUtils.m_resourceManager;
  }

  private void GetVariationsWithPlacementOrigins(
    int rowIndex,
    string varParent,
    ref ArrayList variationsPath)
  {
    for (Node node = ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 2); node != null; node = ((C1FlexGridBase) this).Rows[((RowCol) node.Row).Index].Node.GetNode((NodeTypeEnum) 6))
    {
      if (this.m_gridRows[((RowCol) node.Row).Index] is CApMFVariationGridRow)
      {
        CApMFVariationGridRow gridRow = (CApMFVariationGridRow) this.m_gridRows[((RowCol) node.Row).Index];
        int count = gridRow.VariationOptions.Count;
        this.CreateVariationPath(((RowCol) node.Row).Index, gridRow, varParent, ref variationsPath);
        for (int index = 0; index < count; ++index)
        {
          this.SetActiveOptionsForChildVariations(((RowCol) node.Row).Index);
          string name = ((CApMFVariationOption) gridRow.VariationOptions[index]).Name;
          ((C1FlexGridBase) this).SetData(((RowCol) node.Row).Index, 7, (object) name);
          this.SaveTemporaryVariationRows(((RowCol) node.Row).Index);
          gridRow.SetActiveOption(index);
          this.ReloadVariationHierarchy(((RowCol) node.Row).Index);
          this.GetVariationsWithPlacementOrigins(((RowCol) node.Row).Index, name, ref variationsPath);
        }
      }
      else if (this.m_gridRows[((RowCol) node.Row).Index] is CApMFCompoundResGridRow)
        this.GetVariationsWithPlacementOrigins(((RowCol) node.Row).Index, varParent, ref variationsPath);
      else if (this.m_gridRows[((RowCol) node.Row).Index] is CApMFPrototypeGridRow)
      {
        CApMFPrototypeGridRow gridRow = this.m_gridRows[((RowCol) node.Row).Index] as CApMFPrototypeGridRow;
        if (TxTransformation.op_Inequality(gridRow.PlacementOrigin, (TxTransformation) null))
        {
          foreach (ArrayList arrayList in variationsPath)
          {
            if (varParent.Equals("") || arrayList.Contains((object) varParent))
              ((ArrayList) arrayList[0]).Add((object) gridRow.Name);
          }
        }
      }
    }
  }

  private void CreateVariationPath(
    int currRow,
    CApMFVariationGridRow varRow,
    string varParent,
    ref ArrayList variationsPath)
  {
    int count1 = varRow.VariationOptions.Count;
    if (varParent.Equals(""))
    {
      for (int index = 0; index < count1; ++index)
      {
        ArrayList arrayList = new ArrayList();
        arrayList.Add((object) new ArrayList());
        CApMFVariationGridRow gridRow = this.m_gridRows[currRow] as CApMFVariationGridRow;
        arrayList.Add((object) ((CApMFVariationOption) gridRow.VariationOptions[index]).Name);
        variationsPath.Add((object) arrayList);
      }
    }
    else
    {
      int count2 = variationsPath.Count;
      for (int index1 = 0; index1 < count2; ++index1)
      {
        ArrayList arrayList1 = variationsPath[index1] as ArrayList;
        if (arrayList1.Contains((object) varParent))
        {
          bool flag = false;
          for (int index2 = 0; index2 < count1 && !flag; ++index2)
          {
            if (arrayList1.Contains((object) ((CApMFVariationOption) varRow.VariationOptions[index2]).Name))
              flag = true;
          }
          if (!flag)
          {
            ArrayList arrayList2 = (ArrayList) arrayList1.Clone();
            if (arrayList1[0] is ArrayList)
              arrayList2[0] = ((ArrayList) arrayList1[0]).Clone();
            arrayList1.Add((object) ((CApMFVariationOption) varRow.VariationOptions[0]).Name);
            for (int index3 = 1; index3 < count1; ++index3)
            {
              ArrayList arrayList3 = (ArrayList) arrayList2.Clone();
              if (arrayList2[0] is ArrayList)
                arrayList3[0] = ((ArrayList) arrayList2[0]).Clone();
              arrayList3.Add((object) ((CApMFVariationOption) varRow.VariationOptions[index3]).Name);
              variationsPath.Add((object) arrayList3);
            }
          }
        }
      }
    }
  }

  internal ArrayList GridRows => this.m_gridRows;

  internal bool EditUnitMode => this.m_currentEditedUnit > -1;

  internal bool SaveUnitMode => this.m_saveUnitMode;

  internal bool ConfigurationChanged
  {
    get => this.m_configurationChanged;
    set => this.m_configurationChanged = value;
  }

  internal delegate void CUiMFConfigurationsGridWrapper_LoadConfigurationEventHandler();
}
